<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/admin/login', 'refresh');
		}
		
		$this->load->model('Company_model', 'company');
		$companies = $this->company->get_company_list();
		
		$this->load->model('Intern_type_model', 'intern_type');
		$intern_type = $this->intern_type->get_intern_type();
		
		foreach ($companies as &$company) {
			$company->type_name = $this->intern_type->strid_to_strname($company->type);
		}
		
		//show_error($intern_type[1]);
		// load helper
		$this->load->helper('form');
		$this->load->helper('my');
		
		$this->load->view('admin/index', array(
			"companies" => $companies,
			"intern_type" => $intern_type
		));
	}

	public function del_index(){
		
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/admin/login', 'refresh');
		}
		
		$this->load->model('Company_model', 'company');
		$companies = $this->company->get_del_list();
		
		
		// load helper
		$this->load->helper('form');
		
		$this->load->view('admin/del_index', array(
			"companies" => $companies,
		));
	}
	
	public function show($id){
		
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/admin/login', 'refresh');
		}
		
		$this->load->model('Company_model', 'company');
		$company = $this->company->get_company_info($id);
		
		// change type from id -> name
		$this->load->model('Intern_type_model', 'intern_type');
		$company->type = $this->intern_type->strid_to_strname($company->type);
		
		// change area
		$this->load->model('Area_model', 'area');
		$company->area_id = $this->area->strid_to_strname($company->area_id);
		
		// change what
		$this->load->model('What_model', 'what');
		$company->what_id = $this->what->strid_to_strname($company->what_id);
		
		$company_meta = $this->company->get_company_meta_info($id);
		
		$this->load->view('admin/show', array(
			"company" => $company,
			"company_meta" => $company_meta
		));
	}

	public function edit($id){
		
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/admin/login', 'refresh');
		}
		
		$this->load->model('Company_model', 'company');
		
		// check if test data exist
		//$test_id = $this->company->get_testid_from_id($id);
		//if ( abc ) $id = $test_id;
		
		$company = $this->company->get_company_info($id);
		$company_meta = $this->company->get_company_meta_info($id);
		
		// get all type
		$this->load->model('Intern_type_model', 'intern_type');
		$intern_type = $this->intern_type->get_intern_type();
		
		// get all what name
		$this->load->model('What_model', 'what');
		$what_name = $this->what->get_what_name();
		
		// get all area name
		$this->load->model('Area_model', 'area');
		$area_name = $this->area->get_area_name();
		
		// get data type
		$type = $this->company->get_column_type();
		//show_error($type);
		
		// load helper
		$this->load->helper('form');
		
		$this->load->view('admin/edit', array(
			"company" => $company,
			"company_meta" => $company_meta,
			"intern_type" => $intern_type,
			"what_name" => $what_name,
			"area_name" => $area_name,
			"type" => $type
		));
	}
	
	public function update($id){
		$this->load->model('Company_model', 'company');
		
		$company_fields = $this->company->get_table_field("company");
		$company_meta_fields = $this->company->get_table_field("company_meta");
		
		$company_data = array();
		foreach ($company_fields as $field)if (isset($_POST[$field])) {
			$company_data[$field] = $_POST[$field];
			if ($field === "type") $company_data[$field] = implode(",", $company_data[$field]);
			if ($field === "what_id") $company_data[$field] = implode(",", $company_data[$field]);
			if ($field === "area_id") $company_data[$field] = implode(",", $company_data[$field]);
		}
		
		$company_meta_data = array();
		foreach ($company_meta_fields as $field) {
			if (isset($_POST[$field])) $company_meta_data[$field] = $_POST[$field];
		}
		
		// check if test data exist
		$test_id = $this->company->get_testid_from_id($id);
		if ($test_id == -1){ // non exist, create test data
			$company_data['delete_tag'] = 2;
			$test_id = $this->company->insert_company($company_data, $company_meta_data);
		}
		else $this->company->update_company($test_id, $company_data, $company_meta_data);
		
		$company_data["id"] = $test_id;
		
		//show company page preview
		$this->load->helper('url');
		redirect('/preview/'. $company_data["name_roman"]);
		//show_error($company_data);
		
	}
	public function insert(){
		$this->load->model('Company_model', 'company');
		
		$company_fields = $this->company->get_table_field("company");
		$company_meta_fields = $this->company->get_table_field("company_meta");
		
		$company_data = array();
		foreach ($company_fields as $field) if (isset($_POST[$field])){
			$company_data[$field] = $_POST[$field];
			if ($field === "type") $company_data[$field] = implode(",", $company_data[$field]);
			if ($field === "what_id") $company_data[$field] = implode(",", $company_data[$field]);
			if ($field === "area_id") $company_data[$field] = implode(",", $company_data[$field]);
		}
		
		$company_meta_data = array();
		foreach ($company_meta_fields as $field) {
			if (isset($_POST[$field])) $company_meta_data[$field] = $_POST[$field];
		}
		
		
		// insert test data to preview
		$company_data['delete_tag'] = 2;
		$id = $this->company->insert_company($company_data, $company_meta_data);
		
		//show_error($company_data['delete_tag']);
		
		$company_data["id"] = $id;
		/*$this->load->view('admin/show', array(
			"company" => (object)$company_data,
			"company_meta" => (object)$company_meta_data,
			"message" => "Insert success!"
		));*/
		
		$this->load->helper('url');
		redirect('/preview/'. $company_data["name_roman"]);
	}
	
	public function addnew(){
		
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/admin/login', 'refresh');
		}
		
		$this->load->model('Company_model', 'company');
		
		// get data type of all field
		$company_field_data = $this->company->get_table_field_data("company");
		$company_meta_field_data = $this->company->get_table_field_data("company_meta");
		
		//
		$this->load->model('Intern_type_model', 'intern_type');
		$intern_type = $this->intern_type->get_intern_type();
		
		// get all area name
		$this->load->model('Area_model', 'area');
		$area_name = $this->area->get_area_name();
		
		// get all what name
		$this->load->model('What_model', 'what');
		$what_name = $this->what->get_what_name();
		
		// load helper
		$this->load->helper('form');
		
		$this->load->view('admin/new', array(
			"company_fields" => $company_field_data,
			"company_meta_fields" => $company_meta_field_data,
			"intern_type" => $intern_type,
			"what_name" => $what_name,
			"area_name" => $area_name
		));
	}
	
	public function del($id){
			
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/admin/login', 'refresh');
		}
		
		$this->load->helper('url');
		
		$this->load->model('Company_model', 'company');
		
		if ($this->company->mark_del_company($id)){
			redirect('/admin', 'refresh');
		}
		
	}
	
	public function restore($id){
		$this->load->helper('url');
		
		$this->load->model('Company_model', 'company');
		
		if ($this->company->mark_undel_company($id)){
			redirect('/admin/del_index', 'refresh');
		}
		
	}
	
	public function login(){
		$this->load->view('admin/login', array());
	}
	public function dologin(){
		
		$this->load->helper('url');
		
		if (isset($_POST['pwd']) && $_POST['pwd'] == "berryinc"){
			$this->load->library('session');
			$this->session->set_userdata(array("login" => "logged"));
			
			redirect('/admin/index', 'refresh');
		}
		else{
			redirect('/admin/login', 'refresh');
		}
	}
	public function logout(){
		$this->load->library('session');
		$this->session->unset_userdata("login");
		
		$this->load->helper('url');
		redirect('/admin/login', 'refresh');
	}
	
	public function preview($name){
		
		// check log in
		//$this->load->library('session');
		//if ($this->session->userdata("login") != "logged"){
		//	$this->load->helper('url');
		//	redirect('/admin/login', 'refresh');
		//}
		
		$this->load->model('Company_model', 'company');
		$id = $this->company->get_testid_from_name($name);

		//show_error($id);
		
		$company = $this->company->get_company_info($id);
		$company_meta = $this->company->get_company_meta_info($id);
		
		//show_error(1);
		
		// images string into array
		$company->images = explode(",", $company->images);
		
		// image comment
		$company->img_cmt = explode(",", $company->img_cmt);
		
		$this->load->helper('url');
		$this->load->view('pc/corp', array(
			"preview" => "preview",
			"company" => $company,
			"company_meta" => $company_meta
		));
	}
	
	public function apply_preview($name){
		
		// check log in
		$this->load->library('session');
		if ($this->session->userdata("login") != "logged"){
			$this->load->helper('url');
			redirect('/', 'refresh');
		}
		
		$this->load->model('Company_model', 'company');
		$testid = $this->company->get_testid_from_name($name);
		
		$company = $this->company->get_company_info($testid);
		$company_meta = $this->company->get_company_meta_info($testid);
		
		$id = $this->company->get_id_from_name($name);
		
		//show_error($id);
		
		$this->load->helper('url');
		
		$company->delete_tag = 0;
		unset($company->id);
		unset($company_meta->id);
		
		
		//show_error($_POST["submit"]);
		// if cancel
		if ( $_GET["submit"] === "Cancel" ){
			redirect("/admin/edit/" . $testid);
		}
		else if ($id != -1){	// update existing company
			$company_meta->company_id = $id;
			if ( $this->company->update_company($id, $company, $company_meta) ){
				redirect("/admin/" . $id);
			}
		}
		else{	// really insert new one
			if ($id = $this->company->insert_company( (array)$company, (array)$company_meta) );
				redirect("/admin/" . $id);
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */